﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using ECRMS.WinForm.BaseClass;
using System.Linq;
using DevExpress.XtraTreeList;

namespace ECRMS.WinForm.Modules
{
    [ModuleInstance((int)ModuleEnum.组织架构)]
    public partial class OrgStructureModule : BaseModule
    {
        //public List<Tbl_Tree> trees=new List<Tbl_Tree>();
        public OrgStructureModule() 
        {
            base.core.CreateWaitDialog("初始化中请稍候...");
            InitializeComponent();
            InitView();
            InitTree();
            InitEventHandler();
            base.core.CloseWaitDialog();
        }

        private void InitEventHandler()
        {
            //语言切换
            base.core.LangChanged += new Core.LanguageChangedHandler(this.ChangeLanguage);
        }

        private void InitView()
        {
            //this.Dock = DockStyle.Fill;
        }

        private void InitTree()
        {
            //using (var dataContext = new dbDataContext())
            //{
            //    trees=dataContext.GetTable<Tbl_Tree>().ToList();
            //    this.treeList1.DataSource =  (from d in trees select new { ID = d.ID, Name = d.Name, ParentID = d.ParentID }).ToList();
            //    treeList1.KeyFieldName = "ID";
            //    treeList1.Columns["Name"].Caption="部门名称";
            //    treeList1.ParentFieldName = "ParentID";
            //    treeList1.TreeLineStyle = LineStyle.None;
            //    treeList1.ExpandAll();
            //}
        }

        private void treeList1_FocusedNodeChanged(object sender, FocusedNodeChangedEventArgs e)
        {

            TreeList tree = sender as TreeList;
            TreeListHitInfo hi = tree.CalcHitInfo(tree.PointToClient(Control.MousePosition));
            //if (hi.Node != null)
            //{
            //   //todo
            //}
        }

        //点击节点高亮显示
        private void treeList1_CustomDrawNodeCell(object sender, CustomDrawNodeCellEventArgs e)
        {
            TreeList node = sender as TreeList;
            if (e.Node == node.FocusedNode)
            {
                e.Graphics.FillRectangle(SystemBrushes.Window, e.Bounds);
                Rectangle r = new Rectangle(e.EditViewInfo.ContentRect.Left,
                                            e.EditViewInfo.ContentRect.Top,
                                            Convert.ToInt32(e.Graphics.MeasureString(e.CellText, treeList1.Font).Width + 1),
                                            Convert.ToInt32(e.Graphics.MeasureString(e.CellText, treeList1.Font).Height));

                e.Graphics.FillRectangle(SystemBrushes.Highlight, r);
                e.Graphics.DrawString(e.CellText, treeList1.Font, SystemBrushes.HighlightText, r);

                e.Handled = true;
            }
        }

        private void ChangeLanguage(LangEnum lang)
        {
            //switch (core.NowLanguage)
            switch (lang)
            {
                case LangEnum.China:
                case LangEnum.中文:
                   
                    break;
                case LangEnum.English:
                case LangEnum.英文:
                   
                    break;
                default:
                    break;
            }
        }
    }
}
